//
//  F2DSpriteBackground.m
//  Framework2D
//
//  Created by Silvina Roldan on 04/06/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "F2DSpriteBackground.h"

@implementation F2DSpriteBackground


- (void) setSpriteName:(NSString *)spriteName
{
    _spriteName = spriteName;
    
    if (_atlas != nil)
    {
        SpriteData* spriteData = [_atlas spriteDataFrom:spriteName];
        
        if (spriteData != nil)
        {
            //set vertex position
            _vertices[0].Position[0] = -spriteData->rect.size.width * 0.5f ;
            _vertices[0].Position[1] = -spriteData->rect.size.height * 0.5f;
            
            _vertices[1].Position[0] = spriteData->rect.size.width * 0.5f;
            _vertices[1].Position[1] = -spriteData->rect.size.height * 0.5f;
            
            _vertices[2].Position[0] = -spriteData->rect.size.width * 0.5f;
            _vertices[2].Position[1] = spriteData->rect.size.height * 0.5f;        
            
            _vertices[3].Position[0] = spriteData->rect.size.width * 0.5f;
            _vertices[3].Position[1] = spriteData->rect.size.height * 0.5f;             
            
            //set vertex uvs
            CGSize atlasSize = [_atlas size];
            
            _vertices[0].UV[0] = atlasSize.width - spriteData->rect.size.width;
            _vertices[0].UV[1] = atlasSize.height - spriteData->rect.size.height;;
            
            _vertices[1].UV[0] =  spriteData->rect.size.width;
            _vertices[1].UV[1] = _vertices[0].UV[1];
            
            _vertices[2].UV[0] = _vertices[0].UV[0];
            _vertices[2].UV[1] = atlasSize.height;            
            
            _vertices[3].UV[0] = _vertices[1].UV[0];
            _vertices[3].UV[1] = _vertices[2].UV[1];
            //            _vertices[0].UV[0] = 0;
            //            _vertices[0].UV[1] = 0;
            //
            //            _vertices[1].UV[0] = 1;
            //            _vertices[1].UV[1] = 0;
            //            
            //            _vertices[2].UV[0] = 0;
            //            _vertices[2].UV[1] = 1;
            //
            //            _vertices[3].UV[0] = 1;
            //            _vertices[3].UV[1] = 1;
            
            
            _vertices[0].Color[0] = 1;
            _vertices[0].Color[1] = 1;
            _vertices[0].Color[2] = 1;            
            _vertices[0].Color[3] = 1;                        
            
            _vertices[1].Color[0] = 1;
            _vertices[1].Color[1] = 1;
            _vertices[1].Color[2] = 1;            
            _vertices[1].Color[3] = 1;                        
            
            _vertices[2].Color[0] = 1;
            _vertices[2].Color[1] = 1;
            _vertices[2].Color[2] = 1;            
            _vertices[2].Color[3] = 1;                        
            
            _vertices[3].Color[0] = 1;
            _vertices[3].Color[1] = 1;
            _vertices[3].Color[2] = 1;            
            _vertices[3].Color[3] = 1;                        
        }
    }
}

@end
